var ubahLike = GetXmlHttpObjects();

function GetXmlHttpObjects() {
	if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject) {
		return new ActiveXObjects("Microsoft.XMLHTTP");
	}
	return null;
}

function updateLike(id) {
	if(ubahLike.readyState == 4 || ubahLike.readyState == 0) {
		ubahLike.open("GET","getLike.php?act=1&id="+id,true);
		ubahLike.onreadystatechange = function(){
			var likeXML = ubahLike.responseXML;
			if(likeXML) {
				var like = likeXML.getElementsByTagName("likes")[0];
				document.getElementById("likes-" + like.getElementsByTagName("id")[0].firstChild.nodeValue).innerHTML = like.getElementsByTagName("nlike")[0].firstChild.nodeValue;
				if(like.getElementsByTagName("stat")[0].firstChild.nodeValue == 1) {
					document.getElementById("but_like" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style','background-image: url("theme/like-hover.png")');
					document.getElementById("but_dislike" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style',"");
				} else
					document.getElementById("but_like" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style',"");
			}
		};
		ubahLike.send(null);
	}
}

function updateDislike(id) {
	if(ubahLike.readyState == 4 || ubahLike.readyState == 0) {
		ubahLike.open("GET","getLike.php?act=-1&id="+id,true);
		ubahLike.onreadystatechange = function(){
			var likeXML = ubahLike.responseXML;
			if(likeXML) {
				var like = likeXML.getElementsByTagName("likes")[0];
				document.getElementById("likes-" + like.getElementsByTagName("id")[0].firstChild.nodeValue).innerHTML = like.getElementsByTagName("nlike")[0].firstChild.nodeValue;
				if(like.getElementsByTagName("stat")[0].firstChild.nodeValue == 1) {
					document.getElementById("but_dislike" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style',"background: url('theme/dislike-hover.png')");
					document.getElementById("but_like" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style',"");
				} else {
					document.getElementById("but_dislike" + like.getElementsByTagName("id")[0].firstChild.nodeValue).setAttribute('style',"");
				}
			}
		};
		ubahLike.send(null);
	}
}
